package com.amigo.online.provider.video.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.param.dto.pay.CreditCardPayDto;
import com.amigo.online.common.param.dto.pay.PayPromoteDto;
import com.amigo.online.provider.video.exception.PayException;
import com.amigo.online.provider.video.service.PaymentService;
import com.amigo.online.provider.video.service.PromoteService;

@Api("支付")
@RestController
@RequestMapping("/pay")
public class PaymentController extends BaseController {

    @Autowired
    PromoteService promoteService;
    @Autowired
    PaymentService paymentService;

    @GetMapping("/getClientoken")
    @Deprecated
    public ResponseEntity<Response> getClientoken() {
        return success(paymentService.getClientoken());
    }

    //支付并创建订单
    @Deprecated
    @PostMapping("/createOrder")
    public ResponseEntity<Response> createOrder(@RequestBody PayPromoteDto dto) {
        //参数校验
        if (dto.getPay_money() <= 0) {
            throw new PayException(StatusCode.BAD_REQUEST);
        }
        if (dto.getOrder_id() != null) {
            return success(paymentService.repay(dto));
        }
        JSONObject res = paymentService.createOrder(dto);
        return success(res);
    }

    @ApiOperation(value = "信用卡支付" ,  notes="信用卡支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ACCESS_TOKEN", value = "用户token",paramType = "header",required = true),
    })
    //信用卡支付
    @PostMapping("/creditCardPay")
    public ResponseEntity<Response> creditCardPay(@RequestBody CreditCardPayDto dto) {
        //参数校验
        if (dto.getOrder_id() == null || dto.getOrder_id() == 0) {
            return success(paymentService.creditCardPay(dto));
        }
        return success(paymentService.reCreditCardPay(dto));//重新支付
    }

}
